package com.motionlog.activity;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.TextView;

import com.motionlog.R;
import com.motionlog.model.Database;

public class StatisticDate extends Activity {

	private static final String TAG = null;
	private TextView mMainMotion;
	private TextView mSecondaryMotion;
	private TextView mLogCount;
	private TextView mMainResaon;
	private TextView mMainMethos;
	private TextView mMainCategory;
	private TextView mMethodSuccessRatio;
	private TextView mCategorySuccessRatio;
	private TextView mInfo;

	private int mWeek;

	private Database mDb;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.statistic_date);

		int week = getIntent().getIntExtra("week", -1);
		android.util.Log.d(TAG, "week: " + week);
		if (week == -1) {
			return;
		}

		mMainMotion = (TextView) findViewById(R.id.motion);
		mSecondaryMotion = (TextView) findViewById(R.id.secondary_motion);
		mLogCount = (TextView) findViewById(R.id.log_count);
		mMainResaon = (TextView) findViewById(R.id.main_reason);
		mMainMethos = (TextView) findViewById(R.id.main_method);
		mMainCategory = (TextView) findViewById(R.id.main_category);
		mMethodSuccessRatio = (TextView) findViewById(R.id.method_success_ratio);
		mCategorySuccessRatio = (TextView) findViewById(R.id.category_success_ratio);
		mInfo = (TextView) findViewById(R.id.info);

		mWeek = week;

		mDb = Database.getInstance(this);
		String[] motionStatis = mDb.getMotionStatistic(week);
		mMainMotion.setText(String.format("最主要的情绪: %s", motionStatis[0]));
		mSecondaryMotion.setText(String.format("次要的情绪: %s", motionStatis[1]));
		int logCount = mDb.getLogCount(week);
		mLogCount.setText(String.format("记录总数: %d", logCount));
		mMainResaon.setText(String.format("最主要不良情绪源: %s",
				mDb.getMainReason(week)));
		mMainMethos.setText(String.format("最常用应对方式: %s",
				mDb.getMainMethod(week)));
		mMainCategory.setText(String.format("最主要的认知歪曲: %s",
				mDb.getMainCategory(week)));
		Cursor c = mDb.getFinishMethodCount(week);
		while (c.moveToNext()) {
			int step = c.getInt(0);
			int count = c.getInt(1);

			float ratio = (float) count / (float) logCount * 100f;
			if (step == 1) {
				// method
				mMethodSuccessRatio.setText(String.format("应对方式解决概率: %f%%",
						ratio));
			} else if (step == 2) {
				// category
				mCategorySuccessRatio.setText(String.format("认知方法解决概率: %f%%",
						ratio));
			}
		}
		mInfo.setText("");
	}
}
